package com.wanglibao.woodtrade.woodtrade.ui.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.wanglibao.woodtrade.woodtrade.MyApplication;
import com.wanglibao.woodtrade.woodtrade.R;
import com.wanglibao.woodtrade.woodtrade.model.impl.UserImpl;
import com.wanglibao.woodtrade.woodtrade.service.UpdateService;
import com.wanglibao.woodtrade.woodtrade.ui.activity.user.LoginActivity;
import com.wanglibao.woodtrade.woodtrade.ui.fragment.HomeFragment;
import com.wanglibao.woodtrade.woodtrade.ui.fragment.InformationFragment;
import com.wanglibao.woodtrade.woodtrade.ui.fragment.ShoppingFragment;
import com.wanglibao.woodtrade.woodtrade.ui.fragment.UserFragment;
import com.wanglibao.woodtrade.woodtrade.util.ToastUtil;

import butterknife.Bind;
import butterknife.ButterKnife;

public class HomeActivity extends FragmentActivity implements RadioGroup.OnCheckedChangeListener {

    @Bind(R.id.fl_activity_home_container)
    FrameLayout fl_activity_home_container;

    @Bind(R.id.rg_home_activity_bottom)
    public RadioGroup rg_home_activity_bottom;

    @Bind(R.id.rb_home_activity_home)
    public RadioButton rb_home_activity_home;

    @Bind(R.id.rb_home_activity_shopping)
    public RadioButton rb_home_activity_shopping;

    @Bind(R.id.rb_home_activity_information)
    public RadioButton rb_home_activity_information;

    @Bind(R.id.rb_home_activity_user)
    public RadioButton rb_home_activity_user;

    UserFragment userFragment;

    public FragmentPagerAdapter fragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
        @Override
        public Fragment getItem(int position) {
            Fragment fg = null;
            switch (position) {
                case R.id.rb_home_activity_home:
                    fg = new HomeFragment();
                    break;
                case R.id.rb_home_activity_shopping:
                    fg = new ShoppingFragment();
                    break;
                case R.id.rb_home_activity_information:
                    fg = new InformationFragment();
                    break;
                case R.id.rb_home_activity_user:
                    fg = new UserFragment();
                    break;
            }
            return fg;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            Object fg = super.instantiateItem(container, position);
            if (fg instanceof HomeFragment) {
                HomeFragment homeFragment = (HomeFragment) fg;
                if (homeFragment != null && homeFragment.sv_fragment_home != null) {
                    homeFragment.scrollViewTop();
                    homeFragment.refresh();
                }
            }
            if (fg instanceof ShoppingFragment) {
                ShoppingFragment shoppingFragment = (ShoppingFragment) fg;
                if (shoppingFragment != null) {
                    //调用Fragment中的方法
                    shoppingFragment.refresh();
                }
            }
            if (fg instanceof InformationFragment) {
                InformationFragment informationFragment = (InformationFragment) fg;
                if (informationFragment != null) {
                    //调用Fragment中的方法
                    informationFragment.refresh();
                }
            }
            if (fg instanceof UserFragment) {
                userFragment = (UserFragment) fg;
                if (userFragment != null) {
                }
            }
            return fg;
        }

        @Override
        public int getCount() {
            return 4;
        }
    };

    MyApplication myApplication;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        ButterKnife.bind(this);
        if (Build.VERSION.SDK_INT >= 19) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        myApplication = (MyApplication) getApplication();
        myApplication.addActivity(this);


        initView();
    }


    private void initView() {
        rg_home_activity_bottom.setOnCheckedChangeListener(this);
        rb_home_activity_home.setChecked(true);

        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            UpdateService updateService = new UpdateService(this);
            updateService.checkAppVersion(false);
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        int radioButtonId = group.getCheckedRadioButtonId();
        if (radioButtonId == rb_home_activity_user.getId()) {
            boolean isLogin = new UserImpl(this).isLogin();
            if (!isLogin) {
                startActivity(new Intent(this, LoginActivity.class));
                rb_home_activity_home.setChecked(true);
                return;
            }
        }
        Fragment fragment = (Fragment) fragmentPagerAdapter.instantiateItem(
                fl_activity_home_container, radioButtonId);
        fragmentPagerAdapter.setPrimaryItem(fl_activity_home_container, 0, fragment);
        fragmentPagerAdapter.finishUpdate(fl_activity_home_container);
    }

    private long m_exitTime = 0;

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        int keyCode = event.getKeyCode();
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - m_exitTime) > 2000) {
                ToastUtil.createNormalToast(this, "再点一次退出");
                m_exitTime = System.currentTimeMillis();
            } else {
                myApplication.exit();
                finish();
            }
            return true;
        }
        return super.dispatchKeyEvent(event);
    }

}
